<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>快速点击</button>
    <script>
        /**
         *  节流：将触发频率非常高的操作，变成以一个稳定的频率触发
         * 
         */
        var oBtn = document.querySelector('button');
        var timer = null;
        // oBtn.onclick = function () {
        //     if (timer) {
        //         return;
        //     }
        //     timer = setTimeout(() => {
        //         console.log(1);
        //         timer = null;
        //     }, 1000);
        // }

        oBtn.onclick = throttle(function(e){
            console.log(this.innerHTML);
            console.log(e.target.innerHTML);
        },2000);

        function throttle(fn, delay = 1000) {
            var timer = null;
            return function () {
                if (timer) {
                    return;
                }
                timer = setTimeout(() => {
                    fn.apply(this, arguments);
                    timer = null;
                }, delay);
            }
        }

    </script>
</body>

</html>